All files / src/app/api/collections/[id]/members route.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 2/2
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                      5x 5x 5x 4x   4x 4x   3x 3x   1x               8x 8x 8x 7x   6x   6x 6x   3x 2x   3x      
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { MemberController } from "@/server/api/members";
import { requireApiCapability } from "@/server/dal/collection-access";
import { getDb } from "@/lib/db";
import { errorResponse } from "@/server/api/http-errors";
 
export async function GET(
  _request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const db = getDb();
    // members:read = curator — any curator may list members.
    const access = await requireApiCapability(db, id, user.id, "members:read");
    if (access instanceof NextResponse) return access;
 
    const members = await MemberController.fromDb(db).listMembers(id);
    return NextResponse.json(members);
  } catch (error) {
    return errorResponse(error);
  }
}
 
export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const body = await request.json();
 
    const db = getDb();
    // members:manage = owner — only the collection owner may add members.
    const access = await requireApiCapability(db, id, user.id, "members:manage");
    if (access instanceof NextResponse) return access;
 
    const member = await MemberController.fromDb(db).addMember(id, body);
    return NextResponse.json(member, { status: 201 });
  } catch (error) {
    return errorResponse(error);
  }
}